package com.example.nightmeel;

import com.example.nightmeel.db.TimeLineData;
import com.example.nightmeel.db.TimeLineDbDao;
import com.example.nightmeel.db.TimeLineDbHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailActivity extends Activity implements OnClickListener {

    private static final String tag = DetailActivity.class.getSimpleName();

    private SQLiteDatabase mReadable;

    private ImageView imageview;
    private TextView title;
    private ImageView user_icon;

    private boolean siboFlg = false;

    /**
     * メニューボタン.
     */
    private TextView mSiboView;
    private TextView mLocView;
    private TextView mShareView;
    private TextView mRevView;

    private TextView mSiboCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(tag, "onCreate start.");
        // 初期処理
        init();

        // 渡されたidでDBに問い合わせ
        int rowId = getIntent().getIntExtra("rowId", 1);
        Log.d(tag, "rowId is " + rowId);

        TimeLineDbHelper helper = new TimeLineDbHelper(this);
        mReadable = helper.getReadableDatabase();
        TimeLineDbDao dao = new TimeLineDbDao(mReadable);
        TimeLineData data = dao.selectById(rowId);

        setData(data);

        // dataをUIにセット.
        //setDummy(rowId);
    }

    /**
     * 初期化処理.
     */
    private void init() {
        // 初期処理
        setContentView(R.layout.activity_detail);

        imageview = (ImageView)findViewById(R.id.imageview);
        title = (TextView)findViewById(R.id.detail_title);
        user_icon = (ImageView)findViewById(R.id.user_icon);

        mSiboView = (TextView)findViewById(R.id.sibo_btn);
        mSiboView.setOnClickListener(this);

        mLocView = (TextView)findViewById(R.id.location_btn);
        mLocView.setOnClickListener(this);

        mShareView = (TextView)findViewById(R.id.share_btn);
        mShareView.setOnClickListener(this);

        mRevView = (TextView)findViewById(R.id.reverse_btn);
        mRevView.setOnClickListener(this);

        mSiboCount = (TextView)findViewById(R.id.sibo_count);
        //mSiboCount.setOnClickListener(this);

        // アイコンフォント.
        Typeface mIconic = Typeface.createFromAsset(getAssets(), "font/iconic_stroke.ttf");
        mLocView.setTypeface(mIconic);
        mShareView.setTypeface(mIconic);
    }

    private void setDummy (int rowId) {
        if (rowId == 1) {
            imageview.setImageResource(R.drawable.agemono);
            title.setText("海老フライぺろぺろ");
            user_icon.setImageResource(R.drawable.numa);

        } else if (rowId == 2) {
            imageview.setImageResource(R.drawable.oyakododon);
            title.setText("親子どーん");
            user_icon.setImageResource(R.drawable.piyo);
        } else if (rowId == 3) {
            imageview.setImageResource(R.drawable.kushiage);
        }
    }

    private void setData (TimeLineData data) {

        imageview.setImageURI(Uri.parse(data.getImage()));
        title.setText(data.getMessage());

    }

    @Override
    public void onClick(View v) {
        // 場所
        if (v == mLocView) {
            // インテントのインスタンス生成
            Intent intent = new Intent();
            // インテントにアクション及び位置情報をセット
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("geo:0,0?q=奈良県大和郡山市"));
            // Googleマップ起動
            startActivity(intent);
        // 脂肪数
        } else if (v == mSiboView) {
            if (!siboFlg) {
                // 背景色を変える
                mSiboView.setBackgroundColor(Color.RED);
                mSiboCount.setBackgroundColor(Color.RED);
                //カウントアップ
                String countString = (String) mSiboCount.getText();
                int count = Integer.valueOf(countString);
                count++;
                mSiboCount.setText(Integer.toString(count));

                siboFlg = true;
            }
        } else if (v == mShareView) {
        // 共有
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            // タイトル設定
            intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
            // memoを本文に設定
            intent.putExtra(Intent.EXTRA_TEXT, "title");
            startActivity(intent);

        }else if (v == mRevView) {
        // おえっ
            mRevView.setBackgroundColor(Color.BLUE);
            mRevView.setTextColor(Color.WHITE);
        }
    }
}
